<?php
/**
 * 小程序、APP通用接口
 * 【命名】
 * 方法名：下划线
 * 变量名：小驼峰式
 * 
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG          
 *
 */

use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class apiControl extends mobileHomeControl{

    public function __construct() {
        parent::__construct();
    }
    
    
    /**
     * 获取商品分类列表【一级分类】
     * @author Harris
     * @param gc_id   一级分类id
     * @param page    每页多少条
     * @param curpage 页码
     * @return json
     * */
    public function get_type_listOp($page=10){
    	$_GET['gc_id']?'':output_data(array("code"=>400,"msg"=>"缺少参数gc_id"));
    	$where=array("gc_id_1"=>$_GET['gc_id']);
    	$cp=$list= Model()->table("goods")->page($page)->where($where)->limit(false)->select();
    	foreach($cp as $k => $v){
    		$v['goods_image']=thumb($v,$v['goods_image']);
    		$list[$k]=$v;
    	}
    	output_data($list);
    }
    
	
	/**
	 * 组装活动商品列表【图片地址、砍价/拼团销量】
	 * @author Harris
	 * @param  returnData 商品数组
	 * @param  type 1为砍价2为拼团
	 * @return array
	 * */
	private function make_activity_list($returnData,$type=1){
		$cp=$returnData;
		$saleList=$this->get_sale_count($type);
		$visitList=$this->get_visit_count($type);
		foreach($cp as $k => $v){
    		$returnData[$k]['goods_image']=cthumb($v['goods_image']);
    		$returnData[$k]['sale_count']=$saleList[$v['goods_id']]?$saleList[$v['goods_id']]:0;
    		$returnData[$k]['visit_count']=$visitList[$v['goods_id']]?$visitList[$v['goods_id']]:0;
    	}
		return $returnData;
	}
	
	/**
	 * 获取成功拼团/砍价数量统计
	 * @author Harris
	 * @param  type 1为砍价2为拼团
	 * @return array
	 * */
	private function get_sale_count($type=1){
		if($type==1){
			$list=Model()->query("select goods_id,count(*) from shopnc_order_pintuan where pay_time >0 and lock_state = 1 group by goods_id");
		}else if($type==2){
			$list=Model()->query("select goods_id,count(*) from shopnc_order_kanjia where pay_time >0 and lock_state = 1 group by goods_id");
		}
		$newlist=array();
		foreach($list->fetch_all() as $k => $v){
			$newlist[$v[0]]=$v[1];
		}
		return $newlist;
	}
	
	/**
	 * 获取正在拼团/砍价数量统计
	 * @author Harris
	 * @param  type 1为砍价2为拼团
	 * @return array
	 * */
	private function get_visit_count($type=1){
		if($type==1){
			$list=Model()->query("select goods_id,count(*) from shopnc_order_pintuan where pay_time >0 group by goods_id");
		}else if($type==2){
			$list=Model()->query("select goods_id,count(*) from shopnc_order_kanjia where pay_time >0 group by goods_id");
		}
		$newlist=array();
		foreach($list->fetch_all() as $k => $v){
			$newlist[$v[0]]=$v[1];
		}
		return $newlist;
	}
	
	/**
	 * 获取拼团商品列表
	 * @author Harris
	 * @method GET
	 * @date   2018-09-11 20:00
	 * @param  store_id指定获取某店铺
	 * @param  price   指定获取的价格
	 * @param  hots    获取热门拼团【销量排序】
	 * @param  page    每页查询数量
	 * @param  curpage 分页页码
	 * @return JSON
	 * */
    public function get_pintuan_listOp() {
    	$where=array();
    	$page=$_GET['page']?$_GET['page']:10;
    	$_GET['store_id']?$where['p_pintuan_goods.store_id']=$_GET['store_id']:'';
    	$_GET['hots'] ==1?$order='goods_common.sale_count DESC':'';
    	$_GET['price']?$where['p_pintuan_goods.pintuan_price']=$_GET['price']:'';
    	$table="p_pintuan_goods,p_pintuan,goods,goods_common";
    	$where["p_pintuan_goods.state"]     = 1;
        $where["p_pintuan.state"]           = 1;
    	$where["p_pintuan_goods.pintuan_state"]   = 1;
    	$where["p_pintuan_goods.start_time"]= array('lt',TIMESTAMP);
    	$where["p_pintuan_goods.end_time"]  = array('gt',TIMESTAMP);
    	$on="p_pintuan_goods.pintuan_id=p_pintuan.pintuan_id,p_pintuan_goods.goods_id=goods.goods_id,goods.goods_commonid=goods_common.goods_commonid";
    	$field="p_pintuan_goods.hots,p_pintuan_goods.goods_id,p_pintuan_goods.goods_name,p_pintuan_goods.goods_price,p_pintuan_goods.pintuan_price,
    	p_pintuan_goods.store_id,p_pintuan_goods.start_time,p_pintuan_goods.end_time,p_pintuan_goods.min_num,p_pintuan.pintuan_name,p_pintuan.store_name,goods.goods_image,goods.evaluation_good_star,goods.goods_storage";
    	$returnData=Model()->table($table)->on($on)->where($where)->field($field)->order($order)->page($page)->select();
    	$returnData=$this->make_activity_list($returnData,2);
    	output_data($returnData);
    }
	
	/**
	 * 获取砍价商品列表
	 * @author Harris
	 * @method GET
	 * @date   2018-09-11 20:00
	 * @param  store_id指定获取某店铺
	 * @param  price   指定获取的价格
	 * @param  hots    获取热门砍价【销量排序】
	 * @param  page    每页查询数量
	 * @param  curpage 分页页码
	 * @return JSON
	 * */
    public function get_kanjia_listOp() {
    	$where=array();
    	$page=$_GET['page']?$_GET['page']:10;
    	$_GET['hots']==1?$order='goods_common.sale_count DESC':'';
    	$_GET['store_id']?$where['p_kanjia_goods.store_id']=$_GET['store_id']:'';
    	$_GET['price']?$where['p_kanjia_goods.kanjia_price']=$_GET['price']:'';
    	$table="p_kanjia_goods,p_kanjia,goods,goods_common";
    	$on="p_kanjia_goods.kanjia_id=p_kanjia.kanjia_id,p_kanjia_goods.goods_id=goods.goods_id,goods.goods_commonid=goods_common.goods_commonid";
    	$where["p_kanjia_goods.state"]     = 1;
    	$where["p_kanjia.state"]           = 1;
        $where["p_kanjia_goods.kanjia_state"]= 1;
    	$where["p_kanjia_goods.start_time"]= array('lt',TIMESTAMP);
    	$where["p_kanjia_goods.end_time"]  = array('gt',TIMESTAMP);
    	$field="goods.goods_id,goods.goods_name,goods.goods_price,p_kanjia_goods.kanjia_price,
    	p_kanjia_goods.store_id,p_kanjia_goods.start_time,p_kanjia_goods.end_time,p_kanjia_goods.min_num,p_kanjia.kanjia_name,p_kanjia_goods.store_name,goods.goods_image,goods.evaluation_good_star,goods.goods_storage";
    	$returnData=Model()->table($table)->on($on)->where($where)->field($field)->order($order)->page($page)->select();
        $returnData=$this->make_activity_list($returnData,1);
    	output_data($returnData);
    }
    
    /**
	 * 获取限时商品列表
	 * @author Harris
	 * @method GET
	 * @date   2018-09-11 20:00
	 * @param  store_id指定获取某店铺
	 * @param  price   指定获取的价格
	 * @param  hots    获取热门砍价【销量排序】
	 * @param  page    每页查询数量
	 * @param  curpage 分页页码
	 * @return JSON
	 * */
    public function get_xianshi_listOp() {
    	$where=array();
    	$page=$_GET['page']?$_GET['page']:10;
    	$_GET['hots']==1?$order='goods_common.sale_count DESC':'';
    	$_GET['store_id']?$where['p_xianshi_goods.store_id']=$_GET['store_id']:'';
    	$_GET['price']?$where['p_xianshi_goods.xianshi_price']=$_GET['price']:'';
    	$table="p_xianshi_goods,p_xianshi,goods,goods_common";
    	$on="p_xianshi_goods.xianshi_id=p_xianshi.xianshi_id,p_xianshi_goods.goods_id=goods.goods_id,goods.goods_commonid=goods_common.goods_commonid";
    	$where["p_xianshi_goods.state"]     = 1;
    	$where["p_xianshi.state"]           = 1;
    	$where["p_xianshi_goods.start_time"]= array('lt',TIMESTAMP);
    	$where["p_xianshi_goods.end_time"]  = array('gt',TIMESTAMP);
    	$field="p_xianshi_goods.goods_id,p_xianshi_goods.goods_name,p_xianshi_goods.goods_price,p_xianshi_goods.xianshi_price,
    	p_xianshi_goods.store_id,p_xianshi_goods.start_time,p_xianshi_goods.end_time,p_xianshi.xianshi_name,p_xianshi.store_name,goods.goods_image,goods.evaluation_good_star";
    	$returnData=Model()->table($table)->on($on)->where($where)->field($field)->order($order)->page($page)->select();
    	$returnData=$this->make_activity_list($returnData,1);
    	output_data($returnData);
    }

    /**
     * 获取促销banner
     * @author Harris
     * @param banner_id
     * @return JSON
     * */
    public function get_bannerOp() {
    	$bannerId = isset($_GET['banner_id']) ? (int)$_GET['banner_id'] : false;
    	
        if ($bannerId) {
    		$where = array("ap_id" => $bannerId);
    		$cp = $list = Model("adv")->getList($where, '', 100, "slide_sort asc");

            if (is_array($cp)) {            
        		foreach($cp as $k => $v){
        			$list[$k]['adv_content']="/data/upload/".ATTACH_ADV.'/'.unserialize($v['adv_content'])['adv_pic'];
        			$list[$k]['adv_url']=unserialize($v['adv_content'])['adv_pic_url'];
        		}
            }
    		output_data($list);

    	} else {
    		output_error(array("msg"=>"缺少参数banner_id"));
    	}

    }
    
    /**
     * 获取拼团订单统计
     * @author Harris
     * @param log_id 拼团id
     * @return int
     * */
    private function get_wait_count($logId){
    	$where=array();
    	$where['buyer_type']=$logId;
    	$where['pay_time']=array('gt',0);
    	$count=Model()->table("order_pintuan")->where($where)->count();
    	return $count+1;
    }
    
    /**
     * 待拼团商品列表
     * @author Harris
     * @return JSON
     * */
    public function get_wait_pintuanOp() {
    	$goods_id = $_GET['goods_id'];
    	$key = $_GET['key'];
    	$buyer_id = Model('mb_user_token')->getMbUserTokenInfo(array("token"=>$key))['member_id'];
        if (!$goods_id) {
    		output_error(array("msg"=>"缺少参数goods_id"));
    	}

    	if (!$buyer_id) {
    		output_error(array("msg"=>"缺少参数buyer_id"));
    	}

    	$table = "order_pintuan,p_pintuan_goods,member";
    	$where = array();
        $where['p_pintuan_goods.state']    = 1;
        $where["order_pintuan.lock_state"] = 1;
        $where["order_pintuan.buyer_type"] = 0;
        $where["order_pintuan.end_time"]   = array('gt',TIMESTAMP);
        $where['p_pintuan_goods.goods_id'] = $goods_id;
        $where['order_pintuan.buyer_id']   = array('neq',$buyer_id);
    	$on = "order_pintuan.goods_id=p_pintuan_goods.goods_id,order_pintuan.buyer_id=member.member_id";
        $field = "member.member_avatar,member.member_name,order_pintuan.min_num,order_pintuan.buyer_id,order_pintuan.end_time,order_pintuan.log_id,p_pintuan_goods.goods_name,p_pintuan_goods.goods_price,p_pintuan_goods.pintuan_price,p_pintuan_goods.goods_image,p_pintuan_goods.goods_id";
		$list = Model()->table($table)->on($on)->where($where)->field($field)->limit(10)->select();
        array_walk($list, function(&$item, $keys){
            $item['count'] = $this->get_wait_count($item['log_id']);
            $item['member_avatar'] = getMemberAvatar($item['member_avatar']);
        });

        $where               = array();
        $where['lock_state'] = 1;
        $where['goods_id']   = $goods_id;
        $where["end_time"]   = array('gt',TIMESTAMP);
		$all = Model()->table("order_pintuan")->where($where)->count();
		output_data(array("people" => $all, "pintuan" => $list));
    }
    
    /**
     * 我的砍价列表
     * @author Harris
     * @param  key
     * @return JSON
     * */
    public function my_kanjia_listOp(){
    	$page=$_GET['page']?$_GET['page']:10;
    	$key=$_GET['key']?$_GET['key']:false;
    	$where=array("token"=>$key);
    	$buyer_id=Model("mb_user_token")->getMbUserTokenInfo($where)['member_id'];
    	if($buyer_id){
    		$table="order_kanjia,p_kanjia_goods";
    		$where=array("order_kanjia.buyer_id"=>$buyer_id);
            $where = array(
                'order_kanjia.buyer_id' => array('eq', $buyer_id),
                'order_kanjia.pay_time' => array('gt', 0)
            );
    		$on="order_kanjia.goods_id=p_kanjia_goods.goods_id";
    		$field="order_kanjia.log_id as kanjia_id,order_kanjia.order_id,order_kanjia.buyer_type,order_kanjia.buyer_id,order_kanjia.goods_id,order_kanjia.status,order_kanjia.kanjia_price,order_kanjia.add_time,order_kanjia.end_time,p_kanjia_goods.goods_name,p_kanjia_goods.goods_price,p_kanjia_goods.kanjia_price as goods_kanjia_price,p_kanjia_goods.goods_image,order_kanjia.status";
    		$list = Model()->table($table)->where($where)->on($on)->field($field)->page($page)->order('order_kanjia.add_time DESC')->select();
    		array_walk($list, function(&$item, $key){
                $item['kanjia_price'] = json_decode($item['kanjia_price'], true);
                $item['goods_image'] = thumb($item, 240);
            });
    		output_data($list);
    	}else{
    		output_error(array("msg"=>"缺少参数buyer_id"));
    	}
    	
    }
	
}
